package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhOverdueTrailerMapper;
import com.zjdiepu.www.ibiz.dh.IDhOverdueTrailerService;
import com.zjdiepu.www.model.dh.dto.DhOverdueTrailer;
import com.zjdiepu.www.model.dh.ext.DhOverdueTrailerExt;
import com.zjdiepu.www.model.dh.vo.DhOverdueTrailerVo;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月08日 16时39分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhOverdueTrailerServiceImpl extends BaseService implements IDhOverdueTrailerService {
	public static final Logger logger = LoggerFactory.getLogger(DhOverdueTrailerServiceImpl.class);

	@Autowired
	private DhOverdueTrailerMapper dhOverdueTrailerMapper;
	
	@Override
	public void saveUpdateAndDelete(DhOverdueTrailer dhOverdueTrailer, OperateType operType) {
		switch (operType) {
		case add:
//			dhOverdueTrailer.initInsertData(dhOverdueTrailer.getModifier(), dhOverdueTrailer.getModifierName());
			dhOverdueTrailerMapper.insert(dhOverdueTrailer);
			break;
		case edit:
			dhOverdueTrailer.initUpdateData(dhOverdueTrailer.getModifier(), dhOverdueTrailer.getModifierName());
			// 影响的记录数
			int updateCount = dhOverdueTrailerMapper.updateByPrimaryKeySelective(dhOverdueTrailer);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhOverdueTrailer.initDeleteData(dhOverdueTrailer.getModifier(), dhOverdueTrailer.getModifierName());
			// 影响的记录数
			int deleteCount = dhOverdueTrailerMapper.updateByPrimaryKeySelective(dhOverdueTrailer);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhOverdueTrailer> selectByExt(DhOverdueTrailerExt dhOverdueTrailerExt) {
		return dhOverdueTrailerMapper.selectByExt(dhOverdueTrailerExt);
	}
	
	@Override
	public int countByExt(DhOverdueTrailerExt dhOverdueTrailerExt){
		return dhOverdueTrailerMapper.countByExt(dhOverdueTrailerExt);
	}
	
	@Override
	public DhOverdueTrailer selectById(String id) {
		return dhOverdueTrailerMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhOverdueTrailerMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhOverdueTrailer dhOverdueTrailer) {
		return dhOverdueTrailerMapper.countRefined(dhOverdueTrailer);
	}

	@Override
	public DhOverdueTrailer selectRefined(DhOverdueTrailer dhOverdueTrailer) {
		return dhOverdueTrailerMapper.selectRefined(dhOverdueTrailer);
	}

	@Override
	public boolean repeat(DhOverdueTrailer dhOverdueTrailer) {
		int count = countRefined(dhOverdueTrailer);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhOverdueTrailer.getId())) {
			return true;
		}
		DhOverdueTrailer dbItem = selectRefined(dhOverdueTrailer);
		if(null == dbItem) {
			return false;
		}
		return !dhOverdueTrailer.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueTrailerMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public int countMultiByExt(YwBusinessQueryRequest ywBusinessQueryRequest) {
		return dhOverdueTrailerMapper.countMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public List<DhOverdueTrailerVo> selectMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return dhOverdueTrailerMapper.selectMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public List<DhOverdueTrailerVo> selectExportMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return dhOverdueTrailerMapper.selectExportMultiByExt(ywBusinessQueryRequest);
	}
	
	@Override
	public void insertBatch(List<DhOverdueTrailer> dhOverdueTrailers){
		 dhOverdueTrailerMapper.insertBatch(dhOverdueTrailers);
	}
}
